package priv.bestbeat.reportcenter.core.pdf.chart;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.StandardBarPainter;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Base64;

/**
 * @author 张渠钦
 * @date 2022/4/7 9:45
 */
public class BarChart {

    public static void draw() throws IOException {
        CategoryDataset dataset = createDataset();
        JFreeChart chart = createChart(dataset);
        int width = 800;

        int height = 500;

        BufferedImage bufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

        Graphics2D g2d = bufferedImage.createGraphics();

        RenderedImage rendImage = bufferedImage;
        Rectangle r = new Rectangle(0, 0, 800, 500);
        chart.draw(g2d, r);

//        File f = new File("BarChartDemo1.png");
//
//        ImageIO.write(rendImage, "png", f);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(rendImage,"png",baos);
        String imageBase64 = Base64.getEncoder().encodeToString(baos.toByteArray());
        System.out.println(imageBase64);
    }

    /**
     * Returns a sample dataset.
     *
     * @return The dataset.
     */
    private static CategoryDataset createDataset() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        dataset.addValue(25.00, "企业", "滨江区占杭州市");
        dataset.addValue(69.00, "发明公开专利", "滨江区占杭州市");
        dataset.addValue(12.00, "企业", "滨江区占浙江省");
        dataset.addValue(41.00, "发明公开专利", "滨江区占浙江省");
        dataset.addValue(1.00, "企业", "滨江区占全国");
        dataset.addValue(3.00, "发明公开专利", "滨江区占全国");
        return dataset;
    }

    /**
     * Creates a sample chart.
     *
     * @param dataset  the dataset.
     *
     * @return The chart.
     */
    private static JFreeChart createChart(CategoryDataset dataset) {
        JFreeChart chart = ChartFactory.createBarChart(
                null, null ,
                "百分比（%）" , dataset);

        CategoryPlot plot = (CategoryPlot) chart.getPlot();
        plot.getRangeAxis().setLabelFont(new Font("黑体",Font.BOLD,15));
        plot.getDomainAxis().setLabelFont(new Font("黑体",Font.BOLD,15));
        plot.getDomainAxis().setTickLabelFont(new Font("黑体",Font.BOLD,15));

        NewBarRenderer newBarRenderer = new NewBarRenderer();
        newBarRenderer.setItemMargin(0.1);
        newBarRenderer.setShadowVisible(false);
        newBarRenderer.setBarPainter(new StandardBarPainter());

        plot.setRenderer(newBarRenderer);
        plot.setBackgroundPaint(Color.WHITE);
        plot.setRangeGridlinePaint(Color.GRAY);
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        chart.setBackgroundPaint(Color.WHITE);
        chart.getLegend().setFrame(BlockBorder.NONE);
        chart.getLegend().setItemFont(new Font("黑体",Font.BOLD,15));
        chart.getLegend().setPosition(RectangleEdge.TOP);

        return chart;
    }

}
